// mircoApp/competitionApply/actionStudent/actionStudent.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    formQuery:{
      id:'',
      relation_name:'',
      birthday:'',
      sex:'',
      school:'',
      school_class:'',
      province_id:'',
      city_id:'',
      district_id:'',
      street_id:'',
      address:''
    },
    dyTitle:'',
    iPnoneBottomBol: false, //是否是苹果手机
    sexShow:false,
    radio:'1',
    minDate:  new Date(1970, 0, 1).getTime(),
    maxDate: new Date(2050, 12, 1).getTime(),
    currentDate:new Date().getTime(),
    dateShow:false,
    addressShow:false,
     //街道
     addressName:'',
     imgPath: app.globalData.imgPath,
     formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getiPnoneBottom();
    if(options.id) {
      this.setData({
        'formQuery.id':options.id
      })
      wx.setNavigationBarTitle({
        title: '学员编辑'
      })
      this.goodsDetail();
    } else {
      wx.setNavigationBarTitle({
        title: '新增学员'
      })
      this.initData();
    }
  },
  modelInput(event) {
    const item = event.currentTarget.dataset.item,
          itemValue = event.detail.value;
    console.log(itemValue);
    this.setData({
      [item]:itemValue
    })
  },
  sexPopOn(event) {
    this.setData({
      sexShow:true
    })
    console.log(this.data.sexShow);
  },
  goodsDetail() {
    let urlStr = app.getNetAddresss("plugin.event-registration.api.student.edit");
    app._postNetWork({
      url: urlStr,
      data: {id:this.data.formQuery.id},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          if(data.street_id) {
            this.data.streetName = data.street;
            this.data.strShow = true;
          }
          this.setData({
            'formQuery.relation_name':data.relation_name,
            'formQuery.birthday':data.birthday,
            currentDate:new Date(data.birthday).getTime(),
            'formQuery.sex':data.sex,
            sexName:data.sex == 1 ?'男':'女',
            'formQuery.school':data.school,
            'formQuery.school_class':data.school_class,
            'formQuery.province_id': data.province_id,
            'formQuery.city_id': data.city_id,
            'formQuery.district_id':data.district_id,
            'formQuery.street_id':data.street_id,
            'formQuery.address':data.address,
            addressName:data.province + data.city + data.district,
            streetName:this.data.streetName,
            strShow:true
          })
          
          console.log(this.data.formQuery);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  initData() {
    this.setData({
      strShow:false,
      addressShow:false,
      'formQuery.relation_name':"",
      'formQuery.birthday':'',
      'formQuery.sex':'',
      'formQuery.school':'',
      'formQuery.school_class':'',
      'formQuery.province_id':'',
      'formQuery.city_id':'',
      'formQuery.district_id':'',
      'formQuery.street_id':'',
      'formQuery.address':'',
      sexName:'',
      streetName:'',
      addressName:'',
      sexChecked:false,
      girlChecked:false,
      currentDate:new Date().getTime()
    })
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  addressOn() {
    this.setData({
      addressShow:true
    })
    console.log('322222333');
  },
  getAddress(event) {
    let seleData = event.detail;
    let retCity = seleData[0].name + ' ' + seleData[1].name  + ' ' + seleData[2].name;
    console.log(seleData);
    this.setData({
      addressName: retCity,
      'formQuery.province_id':seleData[0].id,
      'formQuery.city_id':seleData[1].id,
      'formQuery.district_id':seleData[2].id,
      addressShow:false,
      'formQuery.street_id':'',
      streetName:''
    })
    this.getStreet(seleData[2].id);
  },
  streetShowbtn() {
    if(this.is_region == 1 &&  app._isTextEmpty(this.data.formQuery.district_id)) {
      app.tips('请先选择所在地区');
      return;
    } else {
      this.setData({
        getStreet:true
      })
      this.getStreet(this.data.formQuery.district_id);
    }
    this.setData({
      streetShow: true
    });
  },
  streetConfirm(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      streetName: item.areaname,
      streetShow: false,
      'formQuery.street_id': item.id,
    });
  },
  //后台地址的开关控制
  getIsOpenAddress() { 
    let urlStr = app.getNetAddresss("member.member-address.is-region");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          this.setData({
            is_region:data.is_region,
            region_hide:data.region_hide
          })
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //获取街道
  getStreet(param) {
    let that = this;
    let urlStr = "";
    urlStr += app.getNetAddresss("member.memberAddress.street");
    urlStr += '&district_id=' + param;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (!app._isTextEmpty(res.data) && res.data.length > 0) {
            that.setData({
              districtVal: res.data,
              strShow: true,
            });
          } else {
            that.setData({
              strShow: false
            });
          }
        } else {
          that.setData({
            strShow: false
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //提交表单
  studentConfirm() {
    if(app._isTextEmpty(this.data.formQuery.relation_name)) {
      app.tips('请填写学生姓名');
      return;
    }
    if(app._isTextEmpty(this.data.formQuery.birthday)) {
      app.tips('请选择出生日期');
      return;
    }
    if(app._isTextEmpty(this.data.formQuery.sex)) {
      app.tips('请选择性别');
      return;
    }
    if(app._isTextEmpty(this.data.formQuery.school)) {
      app.tips('请填写就读学校名称');
      return;
    }
    if(app._isTextEmpty(this.data.formQuery.school_class)) {
      app.tips('请填写班级名称');
      return;
    }
    if(app._isTextEmpty(this.data.addressName)) {
      app.tips('请选择地区');
      return;
    }
    if(app._isTextEmpty(this.data.streetName)) {
      app.tips('请选择街道');
      return;
    }
    if(app._isTextEmpty(this.data.formQuery.address)) {
      app.tips('请填写详细地址');
      return;
    }
    let urlStr = app.getNetAddresss("plugin.event-registration.api.student.edit-post");
    app._postNetWork({
      url: urlStr,
      data: this.data.formQuery,
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          app.tips(res.msg);
          setTimeout(() => {
            this.goBack();
          },500);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    });
  },
  birthPopOn() {
    this.setData({
      dateShow:true
    })
  },
  sexPopColse() {
    this.setData({
      sexShow:false
    })
  },
  dateCancel() {
    this.setData({
      dateShow:false
    })
  },
  dateConfirm(event) {
    this.setData({
      'formQuery.birthday':this.timestampToTime(event.detail),
      dateShow:false
    })
  },
  sexChange(event) {
    let {name} = event.currentTarget.dataset;
    if(name == 'male') {
      this.data.sexChecked = true;
      this.data.girlChecked = false;
      this.data.formQuery.sex= 1;
      this.data.sexName = '男';
    } else {
      this.data.girlChecked = true;
      this.data.sexChecked = false;
      this.data.formQuery.sex= 2;
      this.data.sexName = '女';
    }
    this.setData({
      sexChecked:this.data.sexChecked,
      girlChecked:this.data.girlChecked,
      'formQuery.sex':this.data.formQuery.sex,
      sexName:this.data.sexName,
      sexShow:false
    })
  },
   //日期转化
   timestampToTime(timestamp) {
    var date = new Date(timestamp);
    let Y = date.getFullYear() + "-";
    let M =
      (date.getMonth() + 1 < 10 ?
        "0" + (date.getMonth() + 1) :
        date.getMonth() + 1) + "-";
    let D = this.change(date.getDate()) + " ";
    return Y + M + D;
  },
  change(t) {
    if (t < 10) {
      return "0" + t;
    } else {
      return t;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})